package com.a2.appa3.ui.fragment;

import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.a2.appa3.R;
import com.a2.appa3.global.HttpAdress;
import com.a2.appa3.global.Info;
import com.a2.appa3.util.SYSOUtils;

/**
 * Created by Administrator on 2019/6/21.
 * 应用中心
 */

public class adFragment extends BaseFragmentSimple {
//    AgentWeb mAgentWeb;

    WebView webview;
    //    private boolean flag = true;
    private String webviewurl;

    public static adFragment newInstance() {

        Bundle args = new Bundle();

        adFragment fragment = new adFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int setContentView() {
        return R.layout.adfragment_layout;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        webview = view.findViewById(R.id.webview);
        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);//允许运行JavaScript
        webSettings.setDomStorageEnabled(true);
        webSettings.setAllowFileAccess(true);
        webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setDatabaseEnabled(true);
        webSettings.setAppCacheEnabled(true);
        webSettings.setSavePassword(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
        webSettings.setUseWideViewPort(true);

        /**
         *   5.0以上
         */
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        }
        String ua = webview.getSettings().getUserAgentString();
        webview.getSettings().setUserAgentString(ua + "; 自定义标记");
        if (!TextUtils.isEmpty(Info.Token)) {
            webview.loadUrl(HttpAdress.adress + "m/#/home?android=true&token=" + Info.Token);
            SYSOUtils.sOut_("加载" + "https://www.ws88168.com/m/#/home?android=true&token=" + Info.Token);
        } else {
            webview.loadUrl(HttpAdress.adress + "m/#/home?android=true&token=null");
        }
        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url.startsWith("http:") || url.startsWith("https:")) {
                    try {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity(intent);
                    } catch (Exception e) {
                        Log.e("xdh", "加载shibai支付宝--" + e);
//                        Toast.makeText(MainActivity2.this, "您没有安装该应用", Toast.LENGTH_LONG).show();
                    }
                }
                return true;
            }

            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
                return super.shouldInterceptRequest(view, request);

            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                //这里我们给提示用户重新启动app
                Toast.makeText(getContext(), "请重新启动App", Toast.LENGTH_SHORT).show();
                handler.proceed();
            }


            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);

            }

            @Override
            public void onPageCommitVisible(WebView view, String url) {
                super.onPageCommitVisible(view, url);
            }

        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        webviewurl = webview.getUrl().toString() + "";
        SYSOUtils.sOut_("+++++++++++++++++返回页面加载的H5页+++++++++++++++++++" + webview.getUrl().toString() + "");
    }


    @Override
    public void onResume() {
        super.onResume();
        if (!TextUtils.isEmpty(webviewurl) && !TextUtils.isEmpty(Info.Token)) {
            if (webviewurl.contains("?android=true")) {
                webview.loadUrl(webviewurl);

            } else {
                webview.loadUrl(webviewurl + "?android=true");
            }
        } else {
            SYSOUtils.sOut_("onresum出问题了++++++++++++++++++++++++++++++++++++++++++++++");
        }
    }
}
